#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangcong
# Email:zc_92@sina.com



from django.forms import Form
from django.forms import widgets
from django.forms import fields

from web import models


# 添加用户 Form
class userprofileForms(Form):
    name = fields.CharField(
        error_messages={
            "required": "姓名不能为空！",
        },
    )

    phone = fields.CharField(
        error_messages={
            "required": "手机号不能为空！",
        },
    )

    sex = fields.CharField(
        error_messages={
            "required": "请选择员工性别！",
        },
    )

    def clean_name(self):
        name = self.data['name']  # 姓名
        obj = models.UserProfile.objects.filter(name=name)

        if obj:
            self.errors['name'] = "姓名已存在!"
        else:
            return self.data['name']

    def clean_phone(self):
        phone = self.data['phone']  # 电话
        if phone.isdigit() and len(phone) == 11:

            obj = models.UserProfile.objects.filter(phone=phone)

            if obj:
                self.errors['phone'] = "手机号已存在!"
            else:
                return self.data['phone']
        else:
            self.errors['phone'] = "手机号格式不对"


# 修改用户 Form
class userprofileForms_update(Form):
    name = fields.CharField(
        error_messages={
            "required": "姓名不能为空！",
        },
    )

    phone = fields.CharField(
        error_messages={
            "required": "手机号不能为空！",
        },
    )

    sex = fields.CharField(
        error_messages={
            "required": "请选择员工性别！",
        },
    )

    id = fields.CharField(
        error_messages={
            "required": "请选择员工性别！",
        },
    )

    def clean_name(self):
        name = self.data['name']  # 姓名
        user_id = self.data['id']  # 用户id

        obj = models.UserProfile.objects.exclude(id=user_id).filter(name=name)

        if obj:
            self.errors['name'] = "姓名已存在!"
        else:
            return self.data['name']

    def clean_phone(self):
        phone = self.data['phone']  # 电话
        user_id = self.data['id']  # 用户id
        if phone.isdigit() and len(phone) == 11:

            obj = models.UserProfile.objects.exclude(id=user_id).filter(phone=phone)

            if obj:
                self.errors['phone'] = "手机号已存在!"
            else:
                return self.data['phone']
        else:
            self.errors['phone'] = "手机号格式不对"
